<template>
    <div>
        <!-- 头部面包屑导航区域 -->
        <el-breadcrumb separator="/">
            <el-breadcrumb-item :to="{ path: '/home ' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>商品管理</el-breadcrumb-item>
            <el-breadcrumb-item>商品列表</el-breadcrumb-item>
        </el-breadcrumb>
        <el-card>
            <el-input placeholder="请输入内容" v-model="queryinfo.query" class="input-with-select">
                <el-button slot="append" icon="el-icon-search" @click="search"></el-button></el-input>
            <el-button class="addbtn" @click="addbtn" type="primary">添加商品</el-button>
            <el-table :data="tableData" border style="width: 100%">
                <el-table-column type="index" label="#">
                </el-table-column>
                <el-table-column prop="goods_name" label="商品名称">
                </el-table-column>
                <el-table-column prop="goods_price" label="商品价格">
                </el-table-column>
                <el-table-column prop="goods_weight" label="商品重量">
                </el-table-column>
                <el-table-column prop="add_time" label="创建时间">
                    <template slot-scope="scope">
                        {{ scope.row.add_time | formdate }}
                    </template>
                </el-table-column>
                <el-table-column prop="address" label="操作">
                    <template slot-scope="scope">
                        <el-button type="primary" @click="EtidUpdata(scope.row.goods_id)">编辑</el-button>
                        <el-button type="danger" @click="DeleteUpdata(scope.row.goods_id)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <!-- 分页 -->
            <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                :current-page="queryinfo.pagenum" :page-sizes="[5, 10, 15, 20]" :page-size="100"
                layout="total, sizes, prev, pager, next, jumper" :total="total">
            </el-pagination>
        </el-card>
    </div>
</template>

<script>
import { Scope } from 'quill'

export default {
  data() {
    return {
      tableData: [],
      // 配置分页
      // 当前页码
      queryinfo: {
        query: '',
        pagenum: 1,
        // 当每页显示条数
        pagesize: 5
      },
      // 总条数
      total: 0,
      // 根据id 查询要修改的参数列表
      goodsid: []
    }
  },
  created() {
    this.getgoodslist()
  },
  methods: {
    async getgoodslist() {
      // 获取表单参数
      const { data: res } = await this.$http.get('goods', {
        params: this.queryinfo
      })
      this.total = res.data.total
      this.tableData = res.data.goods
      this.queryinfo.query = ''
    },
    handleSizeChange(val) {
      this.queryinfo.pagesize = val
      this.getgoodslist()
    },
    handleCurrentChange(val) {
      this.queryinfo.pagenum = val
      this.getgoodslist()
    },
    // 搜索
    search() {
      this.getgoodslist()
    },
    async DeleteUpdata(id) {
      const info = await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (info !== confirm) return this.$message.error('取消删除')
      const { data: res } = await this.$http.delete('goods/' + id)
      console.log(res)
      if (res.meta.status !== 200) return this.$message.error('删除失败')
      this.getgoodslist()
    },
    // 添加商品
    addbtn() {
      this.$router.push('/goods/add')
    }
  }
}
</script>

<style scoped>
.el-input {
    width: 300px !important;
}

.addbtn {
    margin-left: 10px;
}
</style>
